﻿using Model;

namespace Dqs_DAL
{
    public class Teacher_DAL
	{
        /// <summary>
        /// 登录返回一个对象
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
		public TeacherInfo IsLoginOfTeacher(string name, string pwd)
		{
            using MyDbContext db = new MyDbContext();
            var result = db.Teachers.Where(a => a.UserName == name).Where(a => a.Password == pwd).FirstOrDefault();
            if (result != null)
            {
                return result;
            }

            return null!;
        }


		/// <summary>
		/// 查询所有教师信息
		/// </summary>
		/// <returns></returns>
		public List<TeacherInfo> QueryTeacherInfo()
		{
			using (MyDbContext db = new MyDbContext())
			{
				var teachers = db.Teachers.Where(s => s.DelFlag == 0).ToList();
				return teachers;
			}
		}

		#region 教师部分
		/// <summary>
		/// 插入教师信息
		/// </summary>
		/// <param name="teachers"></param>
		public bool InsertTeacherInfo(TeacherInfo teachers)
		{
			using (MyDbContext db = new MyDbContext())
			{
				if (teachers == null)
					return false;
				db.Teachers.Add(teachers);
				db.SaveChanges();
			}
			return true;
		}

		/// <summary>
		/// 通过教师Id修改教师信息
		/// </summary>
		/// <param name="teacherNew"></param>
		public bool UpdateTeacherInfoById(TeacherInfo teacherNew)
		{
			using (MyDbContext db = new MyDbContext())
			{
				var teacherOld = db.Teachers.Where(x => x.Id == teacherNew.Id).FirstOrDefault();
				if (teacherOld == null)
				{
					return false;
				}
				teacherOld.CourseList = teacherNew.CourseList;
				teacherOld.Phone = teacherNew.Phone;
				teacherOld.TeaNum = teacherNew.TeaNum;
				teacherOld.UserName = teacherNew.UserName;
				teacherOld.FullName = teacherNew.FullName;
				teacherOld.Password = teacherNew.Password;
				db.SaveChanges();
			}
			return true;
		}

		/// <summary>
		/// 通过教师Id删除教师信息
		/// </summary>
		/// <param name="teacherId"></param>
		/// <returns></returns>
		public bool DeleteTeacherInfoById(int teacherId)
		{
			using (MyDbContext db = new MyDbContext())
			{
				var teacher = db.Teachers.FirstOrDefault(s => s.Id == teacherId);
				if (teacher == null)
				{
					return false;
				}
				//db.Teachers.Remove(teacher);
				teacher.DelFlag = 1;
				db.SaveChanges();
			}
			return true;
		}

		

		#endregion

	}
}
